package com.hongshan.wallet.router

import android.app.Activity
import android.os.Bundle
import com.alibaba.android.arouter.launcher.ARouter
import com.hongshan.wallet.Constant


/**
 * Copyright Qiandai Ltd.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.router
 * @author   : Administrator
 * @data     : 2019/3/18
 * @email    : zhangxx@baihang-china.com
 */

fun go(
    path: String,
    params: Map<String, Any?>? = null,
    flag: Int? = null,
    activity: Activity? = null,
    requestCode: Int? = null
) {
    val build = ARouter.getInstance().build(path)
    if (params != null) {
        params.forEach {
            when (it.value) {
                is Boolean -> build.withBoolean(it.key, it.value as Boolean)
                is Int -> build.withInt(it.key, it.value as Int)
                is Double -> build.withDouble(it.key, it.value as Double)
                is Char -> build.withChar(it.key, it.value as Char)
                is CharSequence -> build.withCharSequence(it.key, it.value as String)
                is Byte -> build.withByte(it.key, it.value as Byte)
                is Any -> build.withString(it.key,Constant.gson.toJson(it.value))
            }
        }
    }
    if (flag != null) {
        build.withFlags(flag)
    }
    if (activity != null && requestCode != null) {
        build.navigation(activity, requestCode)
    } else {
        build.navigation()
    }
}

fun go(
    path: String,
    params: Bundle?,
    flag: Int? = null,
    activity: Activity? = null,
    requestCode: Int? = null
) {
    var map:Map<String,Any?>? = null
    if (params != null) {
        try {
            val clazz = params.javaClass.superclass
            val field = clazz.getDeclaredField("mMap")
            field.isAccessible = true
            map = field[params] as Map<String, Any?>
        } catch (e:Exception){
            e.printStackTrace()
        }
    }
    go(path,map,flag, activity, requestCode)

}